package com.example.gkcircle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.example.gkcircle.App;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.adapter.BookDetailAdapter;
import com.example.gkcircle.db.DBUtil;
import com.example.gkcircle.library.LibraryRequest;
import com.example.gkcircle.library.object.Book;
import com.example.gkcircle.library.object.DetailBook;
import com.example.gkcircle.utils.ActivityUtil;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.utils.UniversalImageLoader;
import com.example.gkcircle.widget.PullZoomListView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Jehu on 16/4/10.
 */
public class DetailBookActivity extends BaseActivity implements View.OnClickListener{

    private PullZoomListView mListView;
    private BookDetailAdapter mAdapter;
    private View mHeaderView;
    //HeaderView里的控件
    private ImageButton mBackBtn;
    private ImageButton mAddShelfBtn;
    private ImageView mBookImage;
    private TextView  mBookNameTv;
    private TextView  mAuthorTv;
    private TextView  mPublishTv;
    private TextView  mStatusTv;
    private TextView  mLocaltionTv;
    private TextView  mCallNumTv;
    private TextView  mViewCountTv;

    private int position;



    @Override
    protected void initView() {
        setContentView(R.layout.library_book_detail_activity);
        mListView=(PullZoomListView)this.findViewById(R.id.library_book_detail_listview);
        //获取HeaderView
        mHeaderView=mListView.getmHeaderContainer();
        //init view at header
        mBookImage=(ImageView)mHeaderView.findViewById(R.id.library_book_detail_header_image);
        mBackBtn=(ImageButton)mHeaderView.findViewById(R.id.library_book_detail_header_back);
        mAddShelfBtn=(ImageButton)mHeaderView.findViewById(R.id.library_book_detail_header_shelf);
        mBookNameTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_title);
        mAuthorTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_author);
        mPublishTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_publish);
        mStatusTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_status);
        mLocaltionTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_location);
        mCallNumTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_call_num);
        mViewCountTv=(TextView)mHeaderView.findViewById(R.id.library_book_detail_header_view_count);
    }

    @Override
    protected void initListener() {
        mBackBtn.setOnClickListener(this);
        mAddShelfBtn.setOnClickListener(this);
        mBookImage.setOnClickListener(this);
        mListView.setOnRefreshListener(new PullZoomListView.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
    }

    @Override
    protected void setData() {
        Bundle bundle=getIntent().getExtras();
        position=bundle.getInt("position");
        Book book=App.mSearchBookList.get(position);
        mBookNameTv.setText(book.getBookName());
        mAuthorTv.setText(book.getAuthor());
        mPublishTv.setText(book.getPublish());
        mCallNumTv.setText(book.getCallNumber());
        //设置header北影
        mListView.setmHeaderImageResource(R.mipmap.library_book_detail_header_bg);
        if(App.mDetailBookMap==null){
            App.mDetailBookMap=new HashMap<Integer, DetailBook>();
        }
        DetailBook detailBook=new DetailBook();
        detailBook.setIntro("");
        detailBook.setCatalog("");
        detailBook.setTag(new ArrayList<String>());
        App.mDetailBookMap.put(position, detailBook);
        //先设置Adapter
        mAdapter=new BookDetailAdapter(DetailBookActivity.this,App.mDetailBookMap.get(position));
        mListView.setAdapter(mAdapter);
        //获取数据
        LibraryRequest.getInstance().getDetailBookInfo(book.getUrl(), new LibraryRequest.IGetDetailListener() {
            @Override
            public void onSuccess(DetailBook detailBook) {
                App.mDetailBookMap.put(position,detailBook);
                Message msg =new Message();
                if(detailBook!=null){
                    msg.what=0;
                }else{
                    msg.what=1;
                }
                handler.sendMessage(msg);
            }

            @Override
            public void onFailure(int code) {
                Message msg =new Message();
                msg.what=2;
                handler.sendMessage(msg);
            }
        });

    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    if(!dataFlag)return;
                    DetailBook detailBook=App.mDetailBookMap.get(position);
                    mLocaltionTv.setText(detailBook.getLocation());
                    mStatusTv.setText(detailBook.getStatus());
                    mViewCountTv.setText(detailBook.getViews()+"");
                    UniversalImageLoader.getIntance().displayImage(detailBook.getSmallImgUrl(), mBookImage, UniversalImageLoader.getPhotorOptions(), UniversalImageLoader.getAnimateFirstDisplayListener());
                    mAdapter=new BookDetailAdapter(DetailBookActivity.this,detailBook);
                    mListView.setAdapter(mAdapter);
                    mListView.invalidate();
                    break;
                case 1:
                    ToastFactory.getToast(DetailBookActivity.this, getResources().getString(R.string.no_more_data)).show();
                    break;
                case 2:
                    ToastFactory.getToast(DetailBookActivity.this, getResources().getString(R.string.connection_error)).show();
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.library_book_detail_header_image:
                Intent intent=new Intent(this,PhotoViewAcitivty.class);
                Bundle bundle=new Bundle();
                bundle.putString("imageUrl", App.mDetailBookMap.get(position).getBigImgUrl());
                bundle.putString("thumbnailUrl",App.mDetailBookMap.get(position).getSmallImgUrl());
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.library_book_detail_header_back:
                finish();
                break;
            case R.id.library_book_detail_header_shelf:
                //判断是否已登录
                if(AVUser.getCurrentUser()==null){
                    ActivityUtil.startActivity(this, LoginActivity.class);
                    return;
                }
                DBUtil.getInstance(this).addBookToShelf(App.mSearchBookList.get(position),App.mDetailBookMap.get(position).getSmallImgUrl());
                ToastFactory.getToast(this,"已添加到书架").show();
                break;
        }
    }
}
